CREATE TABLE MeterConfiguration
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    MeterID INT NOT NULL REFERENCES Meter(ID),
    DiffID INT NULL REFERENCES MeterConfiguration(ID),
    ConfigKey VARCHAR(50) NOT NULL,
    ConfigText VARCHAR(MAX) NOT NULL
)
GO

CREATE NONCLUSTERED INDEX IX_MeterConfiguration_MeterID
ON MeterConfiguration(MeterID)
GO

CREATE NONCLUSTERED INDEX IX_MeterConfiguration_DiffID
ON MeterConfiguration(DiffID)
GO

CREATE TABLE FileGroupMeterConfiguration
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    FileGroupID INT NOT NULL REFERENCES FileGroup(ID),
    MeterConfigurationID INT NOT NULL REFERENCES MeterConfiguration(ID)
)
GO

CREATE NONCLUSTERED INDEX IX_FileGroupMeterConfiguration_FileGroupID
ON FileGroupMeterConfiguration(FileGroupID)
GO

CREATE NONCLUSTERED INDEX IX_FileGroupMeterConfiguration_MeterConfigurationID
ON FileGroupMeterConfiguration(MeterConfigurationID)
GO

DELETE FROM DataOperation WHERE TypeName = 'FaultData.DataOperations.BreakerRestrikeOperation'
GO

DROP TABLE BreakerRestrike
GO

CREATE TABLE BreakerRestrike
(
    ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
    EventID INT NOT NULL REFERENCES Event(ID),
    PhaseID INT NOT NULL REFERENCES Phase(ID),
    InitialExtinguishSample INT NOT NULL,
    InitialExtinguishTime DATETIME2 NOT NULL,
    InitialExtinguishVoltage FLOAT NOT NULL,
    RestrikeSample INT NOT NULL,
    RestrikeTime DATETIME2 NOT NULL,
    RestrikeVoltage FLOAT NOT NULL,
    RestrikeCurrentPeak FLOAT NOT NULL,
    RestrikeVoltageDip FLOAT NOT NULL,
    TransientPeakSample INT NOT NULL,
    TransientPeakTime DATETIME2 NOT NULL,
    TransientPeakVoltage FLOAT NOT NULL,
    PerUnitTransientPeakVoltage FLOAT NOT NULL,
    FinalExtinguishSample INT NOT NULL,
    FinalExtinguishTime DATETIME2 NOT NULL,
    FinalExtinguishVoltage FLOAT NOT NULL,
    I2t FLOAT NOT NULL
)
GO

CREATE NONCLUSTERED INDEX IX_BreakerRestrike_EventID
ON BreakerRestrike(EventID ASC)
GO

ALTER TABLE NearestStructure
ADD Deviation FLOAT NOT NULL
GO

CREATE NONCLUSTERED INDEX IX_NearestStructure_FaultSummaryID
ON NearestStructure(FaultSummaryID ASC)
GO

CREATE NONCLUSTERED INDEX IX_NearestStructure_StructureID
ON NearestStructure(StructureID ASC)
GO

UPDATE SegmentType SET Name = '3-Phase-to-Ground Fault', Description = 'Fault on all three lines with ground' WHERE Name = '3-PhaseG Fault'
GO

CREATE TABLE [dbo].[PQMarkCompanyCustomer](
	[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[PQMarkCompanyID] [int] NOT NULL FOREIGN KEY REFERENCES PQMarkCompany(ID),
	[CustomerID] [int] NOT NULL
)
GO

ALTER VIEW BreakerHistory
AS
SELECT  Line.ID AS LineID,
		RelayPerformance.EventID AS EventID,
        RelayPerformance.Imax1,
		RelayPerformance.Imax2,
		RelayPerformance.TripInitiate,
		RelayPerformance.TripTime,
		RelayPerformance.PickupTime,
		RelayPerformance.TripCoilCondition,
		RelayAlertSetting.TripCoilCondition AS TripCoilConditionAlert,
		RelayAlertSetting.TripTime AS TripTimeAlert,
		RelayAlertSetting.PickupTime AS PickupTimeAlert,
		RelayPerformance.ChannelID AS TripCoilChannelID
FROM    RelayPerformance LEFT OUTER JOIN
        Channel ON RelayPerformance.ChannelID = Channel.ID LEFT OUTER JOIN
        Line ON Channel.LineID = Line.ID LEFT OUTER JOIN 
		RelayAlertSetting ON RelayAlertSetting.LineID = Line.ID
GO

INSERT INTO Setting(Name, Value, DefaultValue) VALUES('FaultLocation.MinFaultSegmentCycles', '1.0', '1.0')
GO